<?php
/**
 * menueditor.inc
 * Edit custom menues.
 * @copyright	Copyright(c) 2002-2010 Bjorn Winberg
 * @author		Bjorn Winberg <cms@anomalye.net>
 * @license		http://www.gnu.org/licenses/gpl.html GNU General Public License version 2 
 */
class MenuEditor extends Module{
	public static $MODULE_UID = 'menueditor';
	const PARAM_MAX_ACTION = 64;
	const PARAM_MAX_MODULE = 64;
	const PARAM_MAX_MENU = 64;
	const PARAM_MAX_ROW = 5;
	const PARAM_MAX_LABEL = 64;
	const PARAM_MAX_URL = 2000;
	const PARAM_MAX_VISIBILITY = 64;
	const DEFAULT_MODULE = 'system';
	const DEFAULT_MENU = 'MAIN';
	protected $mSystem;	
	
	/**
	 *
	 */
	public function __construct(){
		parent::__construct();
		$this->loadSettings();
		$this->mSystem = ModuleFactory::loadModule('system');	
	}
	
	/**
	 *
	 */
	public function generateView(){
		if(!$this->requireAuthorization('MENUEDITOR/ADMIN')){
			$this->mSystem->feedbackWarn($this->mSystem->localString('ERROR_PERMISSION_DENIED'));
			return false;
		}
		$inputAction = $this->getParamEnc('a', 1, self::PARAM_MAX_ACTION);
		$inputModule = $this->getParamEnc('module', 1, self::PARAM_MAX_MODULE, self::DEFAULT_MODULE);
		$inputMenu = $this->getParamEnc('menu', 1, self::PARAM_MAX_MENU, self::DEFAULT_MENU);
		$localIniPath = $this->getLocalIniFile($inputModule);
		$settingsItem = @parse_ini_file($localIniPath, true);
		if($settingsItem === false){
			$this->mSystem->feedbackWarn($this->localString('ERROR_MODULE_NOT_FOUND'));
			return false;
		}
		$params = array();
		$params['menu'] = $inputMenu;
		$params['module'] = $inputModule;	
		$params['menuItem'] = isset($settingsItem['menu'][$inputMenu])? unserialize(rawurldecode($settingsItem['menu'][$inputMenu])): false;
		if($inputAction === 'create'){
			if(!isset($settingsItem['menu'][$inputMenu])){
				$params['menuItem'] = array();
				$settingsItem['menu'][$inputMenu] = rawurlencode(serialize(array()));
				$settingsModule = ModuleFactory::loadModule('settings');
				$settingsModule->saveSettings($inputModule, $localIniPath, $settingsItem, false);
				
				$menuEditorIniPath = $this->getLocalIniFile('menueditor');
				$menuIniItem = @parse_ini_file($menuEditorIniPath, true);
				$menuEditorMenu = unserialize(rawurldecode($menuIniItem['menu']['MENUES']));
				$newRow['label'] = $inputMenu;
				$newRow['url'] = $this->generateUrl('menueditor', array('menu' => $inputMenu, 'module' => $inputModule), '&');
				$newRow['visibility'] = '';
				$menuEditorMenu[] = $newRow;
				$menuIniItem['menu']['MENUES'] = rawurlencode(serialize($menuEditorMenu));
				$settingsModule->saveSettings('menueditor', $menuEditorIniPath, $menuIniItem, false);
				
				$this->mSystem->feedbackInfo($this->localString('OK_CREATE'));
				$this->mSystem->feedbackLog(static::$MODULE_UID, $this->localString('LOG_CREATE', array($inputModule, $inputMenu)));
				ModuleFactory::loadModule('menueditor', true, true);
			}
			else{
				$this->mSystem->feedbackWarn($this->localString('ERROR_MENU_EXSISTS'));
			}
		}
		elseif($inputAction === 'update'){
			if($params['menuItem'] !== false){
				$inputRow = $this->getParamId('insert', count($params['menuItem']));
				$deleteRow = $this->getParamId('delete', count($params['menuItem']));
				$updateRow = $this->getParamId('update', count($params['menuItem']));
				if($inputRow !== false){
					$newRow['label'] = '';
					$newRow['url'] = '';
					$newRow['visibility'] = '';
					$params['menuItem'] = $this->arrayInsert($params['menuItem'], $inputRow, $newRow);
					$settingsItem['menu'][$inputMenu] = rawurlencode(serialize($params['menuItem']));
					$settingsModule = ModuleFactory::loadModule('settings');
					$settingsModule->saveSettings($inputModule, $localIniPath, $settingsItem, false);
					$this->mSystem->feedbackInfo($this->localString('OK_INSERT'));
					$this->mSystem->feedbackLog(static::$MODULE_UID, $this->localString('LOG_INSERT', array($inputModule, $inputMenu)));
					ModuleFactory::loadModule($inputModule, true, true);
				}
				elseif($deleteRow !== false){
					if(isset($params['menuItem'][$deleteRow])){
						unset($params['menuItem'][$deleteRow]);
						$params['menuItem'] = array_merge(array(), $params['menuItem']);
						$settingsItem['menu'][$inputMenu] = rawurlencode(serialize($params['menuItem']));
						$settingsModule = ModuleFactory::loadModule('settings');
						$settingsModule->saveSettings($inputModule, $localIniPath, $settingsItem, false);
						$this->mSystem->feedbackInfo($this->localString('OK_DELETE'));
						$this->mSystem->feedbackLog(static::$MODULE_UID, $this->localString('LOG_DELETE', array($inputModule, $inputMenu)));
						ModuleFactory::loadModule($inputModule, true, true);
					}
					else{
						$this->mSystem->feedbackWarn($this->localString('ERROR_ROW_NOTFOUND'));
					}
				}
				elseif($updateRow !== false){
					$count = count($params['menuItem']);
					for($i = 0; $i < $count; $i++){
						$inputLabel = $this->getParamEnc('label:' . $i, 1, self::PARAM_MAX_LABEL);
						$inputUrl = $this->getParamEnc('url:' . $i, 1, self::PARAM_MAX_URL);
						$inputVisibility = $this->getParamEnc('auth:' . $i, 1, self::PARAM_MAX_VISIBILITY);
						$newRow = array();
						$newRow['label'] = $inputLabel;
						$newRow['url'] = $inputUrl;
						$newRow['visibility'] = $inputVisibility;
						$params['menuItem'][$i] = $newRow;
					}
					$settingsItem['menu'][$inputMenu] = rawurlencode(serialize($params['menuItem']));
					$settingsModule = ModuleFactory::loadModule('settings');
					$settingsModule->saveSettings($inputModule, $localIniPath, $settingsItem, false);
					$this->mSystem->feedbackInfo($this->localString('OK_UPDATE'));
					$this->mSystem->feedbackLog(static::$MODULE_UID, $this->localString('LOG_UPDATE', array($inputModule, $inputMenu)));
					ModuleFactory::loadModule($inputModule, true, true);
				}
				else{
					$this->mSystem->feedbackWarn($this->localString('ERROR_MENU_NOTFOUND'));
				}
			}
		}
		if($params['menuItem'] === false){
			$this->mSystem->feedbackWarn($this->localString('ERROR_MENU_NOTFOUND'));
			return false;
		}
		return $this->generateTemplate('menueditor_main.inc', $params);
	}
	
	/**
	 *
	 */
	public function firstTimeSetup(){
		$db = ModuleFactory::getDb();
		$auth = ModuleFactory::loadModule('auth');
		$permissionId = $auth->addAction($db, rawurlencode('MENUEDITOR/ADMIN'), 1);
		$auth->grantAction($db, $auth::ADMIN_GROUP_ID, $permissionId);
	}
	
	/**
	 *
	 */
	private function getParamId($aAction, $aRowCount){
		for($i = 0; $i <= $aRowCount; $i++){
			if(isset($_REQUEST[$aAction . ':' . $i])){
				return $i;
			}
		}
		return false;
	}
}
?>
